package com.blate.dogxiao.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.blate.dogxiao.R;
import com.blate.dogxiao.adapter.CitySelectorAdapter;
import com.blate.dogxiao.tools.ScreenSizeTools;

/**
 * 城市列表装饰器
 *
 * @author Az
 * on 2020/11/19
 */
public class CityListDecoration
        extends RecyclerView.ItemDecoration {

    private int mTextMarginStart ;
    private int mGroupTagHeight  ;

    private final Paint mTetPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint mBackgroundPaint = new Paint();

    private final Rect mTextBackgroundRect = new Rect();
    private final Rect mTextBounds = new Rect();

    public CityListDecoration(Context context) {
        mTextMarginStart = ScreenSizeTools.dpToPx(context, 16);
        mGroupTagHeight = ScreenSizeTools.dpToPx(context, 24);
        mTetPaint.setTextSize(context.getResources().getDimension(R.dimen.textSize12));
        mTetPaint.setColor(ContextCompat.getColor(context, R.color.tc4));
        mBackgroundPaint.setColor(ContextCompat.getColor(context, R.color.bc1));
        mBackgroundPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        RecyclerView.Adapter<?> adapter = parent.getAdapter();
        String topTag = null;
        int topTagBottom = mGroupTagHeight;
        if (adapter instanceof CitySelectorAdapter) {
            for (int i = 0; i < parent.getChildCount(); i += 1) {
                View child = parent.getChildAt(i);
                String tag = ((CitySelectorAdapter) adapter).getTag(parent.getChildAdapterPosition(child));
                if (topTag == null) {
                    if (child.getTop() > mGroupTagHeight) {
                        topTag = ((CitySelectorAdapter) adapter).getTopTag(parent.getChildAdapterPosition(parent.getChildAt(0)));
                    }
                }
                if (!TextUtils.isEmpty(tag)) {
                    if (child.getTop() < mGroupTagHeight * 2 && child.getTop() > mGroupTagHeight) {
                        topTagBottom = child.getTop() - mGroupTagHeight;
                    }
                    int tagBottom = child.getTop();
                    mTextBackgroundRect.left = 0;
                    mTextBackgroundRect.top = tagBottom - mGroupTagHeight;
                    mTextBackgroundRect.right = parent.getRight();
                    mTextBackgroundRect.bottom = tagBottom;
                    c.drawRect(mTextBackgroundRect, mBackgroundPaint);
                    mTetPaint.getTextBounds(tag, 0, tag.length(), mTextBounds);
                    c.drawText(tag, mTextMarginStart,
                            tagBottom - (mGroupTagHeight - (mTextBounds.bottom - mTextBounds.top)) / 2.0f,
                            mTetPaint);
                }
            }
            if (topTag == null) {
                topTag = ((CitySelectorAdapter) adapter).getTopTag(parent.getChildAdapterPosition(parent.getChildAt(0)));
            }
            if (topTag != null) {
                mTextBackgroundRect.left = 0;
                mTextBackgroundRect.top = topTagBottom - mGroupTagHeight;
                mTextBackgroundRect.right = parent.getRight();
                mTextBackgroundRect.bottom = topTagBottom;
                c.drawRect(mTextBackgroundRect, mBackgroundPaint);
                mTetPaint.getTextBounds(topTag, 0, topTag.length(), mTextBounds);
                c.drawText(topTag, mTextMarginStart,
                        topTagBottom - (mGroupTagHeight - (mTextBounds.bottom - mTextBounds.top)) / 2.0f,
                        mTetPaint);
            }
        }
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        RecyclerView.Adapter<?> adapter = parent.getAdapter();
        if (adapter instanceof CitySelectorAdapter) {
            String tag = ((CitySelectorAdapter) adapter).getTag(parent.getChildAdapterPosition(view));
            if (tag != null) {
                outRect.top = mGroupTagHeight;
            } else {
                outRect.top = 0;
            }
        }
    }

}
